#include "stdafx.h"
#include "Point2d.h"
#include "VECTOR2d.h"
#include "MATRIX2d.h"
#include "GeomUtils.h"

using namespace stl;

CPoint2D::CPoint2D()
{
	x = 0.0;
	y = 0.0;
}

CPoint2D::CPoint2D(double _x, double _y)
{
	x = _x;
	y = _y;
}

CPoint2D::CPoint2D(const double *pPt)
{
	x = 0.0;
	y = 0.0;
	if (pPt != nullptr)
	{
		x = pPt[0];
		y = pPt[1];
	}
}

CPoint2D::CPoint2D(const POINT2D & p)
{
	x = p.x;
	y = p.y;
}

CPoint2D::CPoint2D(const CPoint2D & pt)
{
	x = pt.x;
	y = pt.y;
}

CPoint2D::~CPoint2D()
{
}

CPoint2D CPoint2D::operator*(const MATRIX2D& matrix) const
{
	double rx, ry, sc;
	rx = x * matrix.A[0][0] + y * matrix.A[1][0] + matrix.A[2][0];
	ry = x * matrix.A[0][1] + y * matrix.A[1][1] + matrix.A[2][1];
	sc = x * matrix.A[0][2] + y * matrix.A[1][2] + matrix.A[2][2];
	rx /= sc;
	ry /= sc;
	return CPoint2D(rx, ry);
}

void CPoint2D::operator*=(const MATRIX2D & matrix)
{
	*this = (*this) * matrix;
}

CPoint2D CPoint2D::operator+(VECTOR2D v) const
{
	return CPoint2D(x + v.x, y + v.y);
}

void CPoint2D::operator+=(VECTOR2D v)
{
	x += v.x;
	y += v.y;
}

CPoint2D CPoint2D::operator-(VECTOR2D v) const
{
	CPoint2D pt(x, y);
	pt.x -= v.x;
	pt.y -= v.y;
	return pt;
}

void CPoint2D::operator-=(VECTOR2D v)
{
	x -= v.x;
	y -= v.y;
}

CVector2D CPoint2D::operator-(POINT2D sp) const
{
	return CVector2D(x - sp.x, y - sp.y);
}


bool CPoint2D::IsEqual(const POINT2D & pt, const CTol & tol) const
{
	if (GeEqual(x, pt.x, tol.TolPtX())
		&& GeEqual(y, pt.y, tol.TolPtY()))
	{
		return true;
	}
	return false;
}

double stl::CPoint2D::DistTo(POINT2D pt) const
{
	CVector2D vec(pt.x - x, pt.y - y);
	return vec.GetLength();
}
